<?php 

class ArticleModel extends BaseModel
{
	private $table;
	
	/**
	 * Hold an instance of the class
	 * @var CommonPageModel
	 */
	private static $_instance = null;
	
	/**
	 * A private constructor
	 * Prevents direct creation of object
	 */
	private function __construct()
	{
		$this->_init();
		$this->table = TABLE_PREFIX . "article";
	}

	/**
	 * The singleton method
	 * @return CommonPageModel
	 */
	public static function getInstance()
	{
		if (!self::$_instance instanceof self)
			self::$_instance = new self;

		return self::$_instance;
	}
	
	public function saveNews($args){

		if(!is_numeric($args["id_article"])) dibi::query("INSERT INTO ".$this->table." ", $args);
		else dibi::query("UPDATE ".$this->table." SET", $args, "WHERE id_article = %i", $args["id_article"]);
	}
	
	public function getNews($cond = array(), $limit = 10){
		$order = array(
        'date' => 'desc'
		);
		
		$news = dibi::query("SELECT * FROM ".$this->table." WHERE %and", $cond ,"ORDER BY %by", $order, "LIMIT 0,$limit")->fetchAll();
		
		return $news;
	}
	
	public function getDataSource(){
		
		$order = array(
        'date' => 'desc'
		);
		
		return dibi::dataSource("SELECT * FROM ".$this->table." ORDER BY %by", $order);
	}
	
	public function getNewById($id_news){
		return dibi::query("SELECT * FROM ".$this->table." WHERE id_article = %i", $id_news)->fetch();
	}
	
	public function deleteNews($id_news){

		dibi::query("DELETE FROM ".$this->table." WHERE id_article = %i", $id_news);

	}
	
	public function getNewByName($name){
		return dibi::fetch("SELECT * FROM {$this->table} WHERE seo_title = %s", $name);
	}
	
	public function getPicture($id_file){
		return dibi::fetch("SELECT * FROM " . TABLE_FILES . " WHERE id_file = %i", $id_file);
	}
}

?>
